package com.hhxy.login.servlet;

import com.hhxy.login.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ghp
 * @date 2022/9/26
 * @Title
 * @description
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//获取来自用户输入的验证码
		String checkCode = request.getParameter("check_code");
		//获取CheckServlet存储在Session域中的验证码
		String saveCode = (String) request.getSession().getAttribute("check_code");
		PrintWriter pw = response.getWriter();
          //假设正确的用户名 是itcast 密码是123
		if (("itcast").equals(username) && ("123").equals(password) && checkCode.equals(saveCode)) {
			//用户名、密码、验证码都正确，则直接跳转到IndexServlet
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			request.getSession().setAttribute("user", user);
			response.sendRedirect("/day04_Fifth/indexServlet");
		} else if(checkCode.equals(saveCode)){
			pw.write("用户名或密码错误，登录失败");
		}else {
			pw.write("验证码错误");
		}
    }
}